package day1;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * ClassName：TestUDPCilent
 *
 * @Author：小鱼
 * @Create：2025-08-25 14:12
 */
public class TestUDPClient {
    //客户端
    public static void main(String[] args) {
        String ip = "127.0.0.1";
        int port = 9999;
        DatagramSocket socket = null;
        DatagramPacket packet = null;
        try {
            //负责发数据
            socket = new DatagramSocket();
            //准备要发的数据
            byte[] buff = "hello world".getBytes();
            packet = new DatagramPacket(buff,0,buff.length, InetAddress.getByName(ip),port);
            //发送数据
            socket.send(packet);
            System.out.println("客户端发生数据成功：");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭资源
            if(socket!=null){
                socket.close();
            }

        }

    }
}
